<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns:th="http://www.thymeleaf.org" xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<base th:href="@{/}"><!-- 不用base就使用th:src="@{/js/jquery.min.js} -->
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title>天天生鲜-用户中心</title>
	<link rel="stylesheet" type="text/css" href="css/reset.css">
	<link rel="stylesheet" type="text/css" href="css/main.css">
	<script src="js/jquery-1.12.4.min.js"></script>
	<script src="js/ajaxfileupload.js"></script>
	<script src="/layui/layui.js"></script>

</head>
<body>
	<div class="header_con">
		<div class="header">
			<div class="welcome fl">欢迎来到天天生鲜!</div>
			<div class="fr">
				<div class="login_info fl">
					欢迎您：<em>张 山</em>
				</div>
				<div class="login_btn fl">
					<a href="login.html">登录</a>
					<span>|</span>
					<a href="register.html">注册</a>
				</div>
				<div class="user_link fl">
					<span>|</span>
					<a href="user_center_info.html">用户中心</a>
					<span>|</span>
					<a href="cart.html">我的购物车</a>
					<span>|</span>
					<a href="user_center_order.html">我的订单</a>
				</div>
			</div>
		</div>		
	</div>

	<div class="search_bar clearfix">
		<a href="index.html" class="logo fl"><img src="images/logo.png"></a>
		<div class="sub_page_name fl">|&nbsp;&nbsp;&nbsp;&nbsp;商家中心</div>
		<!-- <div class="search_con fr">
			<input type="text" class="input_text fl" name="" placeholder="搜索商品">
			<input type="button" class="input_btn fr" name="" value="搜索">
		</div>		 -->
	</div>

	<div class="main_con clearfix">
		<div class="left_menu_con clearfix">
			<h3>商家中心</h3>
			<ul>
				<li><a href="/toSellerCenterInfo" >· 店铺信息</a></li>
				<li><a href="/toGoodsInfo">· 商品信息</a></li>
				<li><a href="/seller/QueryOrder">· 订单信息</a></li>
				<li><a href="/seller/comments">· 评论</a></li>
			</ul>
		</div>
		<div class="right_content clearfix">
				<h3 class="common_title2">新增商品</h3>
				
				<div class="site_con">
					<form>
						<input type="hidden" th:value="${good.id}" id="goodId"/>
						<div class="form_group">
							<label>类型：</label>
							<select  name="typeId" id="typeId" style="position: relative;top: 10px;">
								<option th:each="goodsType:${goodTypes}" th:text="${goodsType.typeName}" th:value="${goodsType.id}"></option>
							</select>
						</div>
						<div class="form_group" >
							<label>名称：</label>
							<input type="text" name="name" th:value="${good.name}" id="goodName">
						</div>
						<div class="form_group" style="height: 130px;">
							<label>图片：</label>
							<div id="image" class="image">
								<img th:src="'/images/'+${good.picture}" width="100px" height="100px"
									 style="position: relative;bottom:40px;"/>
							</div>
							<input type="file" class="button" name="file" value="选择图片" id="pictureBtn" onchange="pictureChange()" />
						</div>
						<div class="form_group form_group2">
							<label>详情：</label>
							<textarea class="site_area" name="goodDetail" id="goodDetail" th:text="${good.goodDetail}"></textarea>
						</div>
						<div class="form_group">
							<label>数量：</label>
							<input type="text" name="store" th:value="${good.store}" id="goodNum">
						</div>
						<div class="form_group">
							<label>价格：</label>
							<input type="text" th:value="${good.newPrice}" name="newPrice" id="goodPrice">
						</div>
<!--						<input type="submit" name="" value="提交" class="info_submit">-->
						<button id="updateBtn" type="button" class="info_submit">提交</button>

					</form>
				</div>
		</div>
	</div>



	<div class="footer">
		<div class="foot_link">
			<a href="#">关于我们</a>
			<span>|</span>
			<a href="#">联系我们</a>
			<span>|</span>
			<a href="#">招聘人才</a>
			<span>|</span>
			<a href="#">友情链接</a>		
		</div>
		<p>CopyRight © 2016 北京天天生鲜信息技术有限公司 All Rights Reserved</p>
		<p>电话：010-****888    京ICP备*******8号</p>
	</div>
	
</body>
<script>
	$(function (){
		//检查商品名称
		$("#goodName").blur(function (){
			var goodName = $("#goodName").val();
			$.ajax({
				url:"/goodstable/checkName",
				data:{"goodName":goodName},
				type:"get",
				dataType:"json",
				success:data =>{
					if(data.code != 200)
					{
						layer.alert(data.message);
					}
				}
			})
		});

		// 检查商品详情
		$("#goodDetail").blur(function (){
			var goodDetail = $("#goodDetail").val();
			$.ajax({
				url:"/goodstable/checkGoodDetail",
				data:{"goodDetail":goodDetail},
				type:"get",
				dataType:"json",
				success:data =>{
					if(data.code != 200)
					{
						layer.alert(data.message);
					}
				}
			})
		});

		// 检查商品数量
		$("#goodNum").blur(function (){
			var goodNum = $("#goodNum").val();
			$.ajax({
				url:"/goodstable/checkGoodNum",
				data:{"goodNum":goodNum},
				type:"get",
				dataType:"json",
				success:data =>{
					if(data.code != 200)
					{
						layer.alert(data.message);
					}
				}
			})
		});

		// 检查商品价格
		$("#goodPrice").blur(function (){
			var goodPrice = $("#goodPrice").val();
			$.ajax({
				url:"/goodstable/checkGoodPrice",
				data:{"goodPrice":goodPrice},
				type:"get",
				dataType:"json",
				success:data =>{
					if(data.code != 200)
					{
						layer.alert(data.message);
					}
				}
			})
		});

		//添加
		$("#updateBtn").click(function (){
			var id = $("#goodId").val();
			var name = $("#goodName").val();
			var newPrice = $("#goodPrice").val();
			var store = $("#goodNum").val();
			var typeId = $("#typeId").val();
			var goodDetail = $("#goodDetail").val();
			$.ajax({
				url:"/goodstable/updateGood",
				data:{"name":name,"newPrice":newPrice,"store":store,"typeId":typeId,"goodDetail":goodDetail,"id":id},
				type:"post",
				dataType:"json",
				success:data =>{
					if(data.code != 200)
					{
						layer.alert(data.message);
					}else {
						location.href="/toGoodsInfo"
					}
				}
			})

		});

	})
	function pictureChange(){
		$.ajaxFileUpload({
			url:"/goodstable/picture",
			secureuri:false,
			dataType:"json",
			fileElementId:"pictureBtn",
			success:function (data){
				$.ajax({
					url:"/goodstable/getPrictureUrl",
					type:"get",
					dataType: "json",
					success:function (data){
						if(data.code != 200)
						{
							alert(data.message)
						}else {
							$("#image").empty(); //清空原有数据
							var imgObj = $("<img>"); //创建img 标签对象
							//给img标签对象添加属性
							imgObj.attr("src","/images/"+data.message);
							imgObj.attr("width","100px");
							imgObj.attr("height","100px");
							imgObj.attr("style","position:relative;bottom:40px;")
							$("#image").append(imgObj);
						}
					}

				})

			}
		})
	}
</script>
</html>

<style type="text/css">
	.image{
		height: 100px;
		width: 100px;
		position: relative;
		left: 100px;
	}
	.button{
		position: relative;
		margin-top: 10px;
		left: 100px;
	}
</style>